
$(function(){

    $('#side-menu ul').eq(0).addClass('in');

    var carLength = {
        data: [
            { name: '请选择车长', value: '' },
            { name: '3.8米', value: '3.8米' },
            { name: '4.2米', value: '4.2米' },
            { name: '4.5米', value: '4.5米' },
            { name: '5米', value: '5米' },
            { name: '5.2米', value: '5.2米' },
            { name: '6.2米', value: '6.2米' },
            { name: '6.8米', value: '6.8米' },
            { name: '7.2米', value: '7.2米' },
            { name: '7.6米', value: '7.6米' },
            { name: '8.2米', value: '8.2米' },
            { name: '8.6米', value: '8.6米' },
            { name: '9.6米', value: '9.6米' },
            { name: '11.7米', value: '11.7米' },
            { name: '12.5米', value: '12.5米' },
            { name: '13米', value: '13米' },
            { name: '13.5米', value: '13.5米' },
            { name: '14米', value: '14米' },
            { name: '15米', value: '15米' },
            { name: '16米', value: '16米' },
            { name: '17米', value: '17米' },
            { name: '17.5米', value: '17.5米' },
            { name: '18米', value: '18米' }
        ]
    };
    for (var i = 0; i < carLength.data.length; i++) {
        $('#carLength').append('<option value="' + carLength.data[i].value + '">' + carLength.data[i].name + '</option>');
    }

    $('#btn_add_cuser').on('click',function(){
        openPlatform();
        var validFlag = $('#add_cuser_form').valid();
        if (validFlag) {
            addCUserInfo();
        }else{
            return false;
        }
    });

    //选中平台验证日期 1、iOS  2、android
    $('#i_login_type').on('click',function(){
        if($("#i_login_type").is(':checked')){
            $("#i_stopTime").prop("required", "true");
            $("#i_login_type").val(1);
        }else{
            $("#i_stopTime").removeAttr("required");
        }

    });

    $('#a_login_type').on('click',function(){
        if($("#a_login_type").is(':checked')){
            $("#a_stopTime").prop("required", "true");
            $("#a_login_type").val(2);
        }else{
            $("#a_stopTime").removeAttr("required");
        }

    });

    /**
     *  功能描述：添加用户验证
     */

    $('#add_cuser_form').validate({
        errorElement:'span',
        errorClass:'error',
        focusInvalid:false,
        ignore:'',
        rules:{
            userName:{
                required:true
            },
            password: {
                required: true,
                rangelength:[6,16]
            },
            certName:{
                required: true
            },
            phone:{
                required: true
            },
            carLength:{
                required: true
            },
            carType:{
                required: true
            },
            runningRoute:{
                required: true
            }
        },
        messages:{

            userName:{
                required:'请输入帐号'
            },
            password:{
                required:'请输入密码',
                rangelength:'密码长度必须在6-16个字符之间'
            },
            certName:{
                required: '请输入姓名'
            },
            phone:{
                required: '请输入随车电话'
            },
            carLength:{
                required: '请输入车长'
            },
            carType:{
                required: '请输入车型'
            },
            runningRoute:{
                required: '请输入常跑路线'
            }
        },
        invalidHandler:function(event,validator){
            $('.alert-success').hide();
            $('.alert-error').show();
        },
        highlight:function(element){
            $(element).closest('.help-inline').removeClass('ok');
            $(element).closest('.form-group').removeClass('success').addClass('error');
        },
        unhighlight:function(element){
            $(element).closest('.form-group').removeClass('error');
        },
        success:function(label){
            label.addClass('valid').addClass('help-inline ok').closest('.form-group').removeClass('error').addClass('success');
        },
        submitHandler:function(form){
            $('.alert-success').show();
            $('.alert-error').hide();
        }
    });


});

/**
 *  功能描述：添加车主
 *  请求方式：POST
 *  请求地址：/api/cuser/edit
 *  函数名称：addCUserInfo
 */
function addCUserInfo(){
    $.ajax({
        url:whole_path+'/api/cuser/edit',
        type:'POST',
        dataType:'json',
        data:$('#add_cuser_form').serialize(),
        beforeSend:function(){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:true,canCance:true});
        },
        success:function(data){
            if(data.status == 0){
                $.toast('操作成功',1000);
                setTimeout(function(){
                    window.location.href = 'cuser_list.jsp';
                },1000);
            }else {
                $.toast('操作失败,系统错误',1000);
            }
        },
        complete:function(){
            $.progressBar().close();
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    });
}

//校验用户是否已存在
$("#c_user_name_id").blur(function(){
    $.ajax({
        url:whole_path+'/api/cuser/detail',
        type:'GET',
        cache:false,
        dataType:'json',
        data:{
            userName:$('#c_user_name_id').val()
        },
        beforeSend:function(){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:true,canCance:true});
        },
        success:function(data){
            if(data.status == 0){
                if (JSON.stringify(data.data) != "{}") {
                    var txt = "此用户已存在,请重新输入！";
                    window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.success);
                    $('#c_user_name_id').val('');
                }
            }else {
                $.toast('操作失败,系统错误',1000);
            }
        },
        complete:function(){
            $.progressBar().close();
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    });
})

//拼接开通平台类型数组
function openPlatform() {
    var expireTimes = [];
    $("#cUserPlatformContain").find(".form-group").each(function(index, item) {
        //将数据拼成对象放进expireTime
        var expireTime = {
            'platformType': $("input", item).eq(0).val(),
            'expire_time': $("input", item).eq(1).val()
        };
        //将对象存进expireTimes数组
        expireTimes.push(expireTime);
    });
    //数组转成JSON
    var json = JSON.stringify(expireTimes);
    //平台类型到期时间的参数放到隐藏域内
    $('input[name="expireTime"]').val(json);
}
